<?php

class ClientController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        $this->_helper->layout->setLayout('layout-mantenimientos');
    }

    public function indexAction()
    {
        // action body
        try{
            // action body
            $customers = new Application_Model_DbTable_Client();
            $this->view->clientes = $customers->find();
            if ($this->getRequest()->isPost()) {
                $ced = $_POST['cedula'];
                $name = $_POST['nombre'];
                if(!empty($ced) || !empty($name)){                    
                    $this->view->clientes = $customers->findClient($ced, $name);
                }
            }

        }
        catch(Exception $e){
            //   $logger = Zend_Registry::get('logger');     
           // $logger->log($e->getMessage()."; File Name: ".$e->getFile ( )."; Line: ".$e->getLine ( ), Zend_Log::ERR);
            throw $e;
        }
    }

   
    

    public function addAction()
    {
        // action body
        
        if ($this->getRequest()->isPost()) {
            if(!empty($_POST['nombre'])){
                $ced = $_POST['cedula'];
                $name = $_POST['nombre'];
                $fecha = $_POST['date'];
                $phone = $_POST['telefono']; 
                $email = $_POST['correo'];
                $vehiculos = $_POST['vehiculo']; 
                           
                $customers = new Application_Model_DbTable_Client();
                $vehicleClient = new Application_Model_DbTable_VehicleClient();
                $vehicle = new Application_Model_DbTable_Vehicle();
                $customers->addClient($ced, $name,$fecha, $phone, $email);

                //Se insertan los vehiculos seleccionados para el cliente
                if(null != $vehiculos){
                    foreach(array_keys($vehiculos) as $i) { 
                        # code...
                        $vehiculo = $vehiculos[$i];
                        $vehicleClient->addVehicleClient($vehiculo, $ced);
                        $vehicle->updateAssigned($vehiculo, 'A');
                    }    
                }
                
            }      
        }

        //Se busca los vehiculos no asignados a clientes
        $vehicle = new Application_Model_DbTable_Vehicle();
        $this->view->vehicles = $vehicle->findNotAssigned();
                    
    }   

     public function editAction()
    {
        // action body
        
       
            $id = $this->_getParam('id', 0);

            if ($id != null) {
                try{
                    //Se busca la informacion del cliente
                    $customers = new Application_Model_DbTable_Client();                                
                    $this->view->client = $customers->getClient($id);
                    
                    //Se busca los vehiculos no asignados a clientes
                    $vehicle = new Application_Model_DbTable_Vehicle();
                    $this->view->vehicles = $vehicle->findNotAssigned();
                  
                    //Se buscan los vehiculos asignados al cliente
                    $vehicleClient = new Application_Model_DbTable_VehicleClient();
                    $myVehicles = $vehicleClient->findUserVehicle($id);

                    $values = array();
                    foreach ($myVehicles as $key => $vehicle) {                        
                        # code...
                        $values[$key] = $vehicle['placa'];
                    }
                    if(!empty($values)){
                        //Se buscan los vehiculos asignados al cliente
                        $vehicle = new Application_Model_DbTable_Vehicle();
                        $this->view->myVehicles = $vehicle->findUserVehicle($values);
                    }
                }
                catch(Exception $e){
                    throw $e;
                }             

            if(!empty($_POST['nombre'])){   
                $ced = $_POST['cedula'];
                $name = $_POST['nombre'];
                $fecha = $_POST['date'];
                $phone = $_POST['telefono']; 
                $email = $_POST['correo'];    
                $vehiculos = $_POST['vehiculo'];  

                try{
                    $customers = new Application_Model_DbTable_Client();
                    $customers->updateClient($id,$ced,$name,$fecha, $phone, $email);
                    
                    $vehicle = new Application_Model_DbTable_Vehicle();
                    $vehicleClient = new Application_Model_DbTable_VehicleClient();

                    $myVehicles = $vehicleClient->findUserVehicle($id);

                    //Se actualiza el vehiculo a estado no asignado
                    foreach ($myVehicles as $key => $myVehicle) {                        
                        # code...
                        $vehicle->updateAssigned($myVehicle['placa'], 'N');
                    }

                    //Se elimina los vehiculos relacionados del cliente
                    $vehicleClient->deleteVehicles($id);

                    //Se insertan los vehiculos seleccionados para el cliente
                    foreach(array_keys($vehiculos) as $i) { 
                        # code...
                        $vehiculo = $vehiculos[$i];
                        $vehicleClient->addVehicleClient($vehiculo, $id);
                        $vehicle->updateAssigned($vehiculo, 'A');
                    }

                    $this->_helper->redirector('index');
                }
                catch(Exception $e){
                    throw $e;
                }
            }
       
        }
    }

     public function deleteAction()
    {
       
        $ced = $this->getRequest()->getPost('id');
        try{
            $customers = new Application_Model_DbTable_Client();
            $customers->deleteClient($ced);
            $this->_helper->redirector('index');
        }
        catch(Exception $e){          
            throw $e;
        }
           
    }

}



